package com.douxg.learning.springboot.es;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryStringQuery;
import co.elastic.clients.elasticsearch.core.GetResponse;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.sql.ElasticsearchSqlClient;
import co.elastic.clients.elasticsearch.sql.QueryRequest;
import co.elastic.clients.elasticsearch.sql.QueryResponse;
import co.elastic.clients.json.JsonData;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.StringReader;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description
 * @Auther douxg
 * @Date 2022/8/23 19:19
 */
@RestController
@RequestMapping("/search")
public class EsController {
    @Resource
    private ElasticsearchClient elasticsearchClient;
//    @Resource
//    private EsDataSource esDataSource;

    @GetMapping("id")
    public Object id(@RequestParam Map<String, Object> params) throws IOException {
        String index = params.get("index").toString();
//        List<String> list = Arrays.asList(map.get("indexs").split(","));
        GetResponse<JsonData> response = elasticsearchClient.get(builder -> builder.index(index).id(params.get("id").toString()), JsonData.class);
//        SearchResponse<JsonData> response = elasticsearchClient.search(builder -> builder.index(list).size(10), JsonData.class);
        return response.source().toString();
    }

    @GetMapping("query")
    public Object query(@RequestParam Map<String, Object> params) throws IOException {
        List<String> list = Arrays.asList(params.get("indexs").toString().split(","));
        SearchResponse<Map> response = elasticsearchClient.search(builder -> builder.index(list)
                .query(builder1 -> builder1.queryString(QueryStringQuery.of(builder2 -> builder2.query(params.get("query").toString())))), Map.class);
        List<Map> collect = response.hits().hits().stream().map(jsonDataHit -> jsonDataHit.source()).collect(Collectors.toList());
        return collect;
    }

    /**
     * 需要充值到基础级
     *
     * @param sql
     * @return
     * @throws IOException
     */
    @GetMapping("sql")
    public Object sql(@RequestParam String sql) throws IOException {
        ElasticsearchSqlClient sqlClient = elasticsearchClient.sql();
        QueryRequest.Builder builder1 = new QueryRequest.Builder();
        builder1.format("json");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("query", sql);
        String s = jsonObject.toJSONString();
        builder1.withJson(new StringReader(s));
        QueryResponse query1 = sqlClient.query(builder1.build());
        QueryResponse query = sqlClient.query(builder -> builder.query(sql).format("json"));
//        SearchResponse<JsonData> response = elasticsearchClient.search(builder -> builder.query(builder1 -> builder1.queryString(QueryStringQuery.of(builder2 -> builder2.query(sql)))), JsonData.class);
//        List<JSONObject> collect = response.hits().hits().stream().map(jsonDataHit -> jsonDataHit.source()).map(jsonData -> jsonData.toString()).map(s -> JSONObject.parseObject(s)).collect(Collectors.toList());
        return query.columns();
    }

    /**
     * 需要充值到白金
     *
     * @param sql
     * @return
     * @throws IOException
     * @throws SQLException
     */
//    @GetMapping("dssql")
//    public Object dssql(@RequestParam String sql) throws IOException, SQLException {
//        Connection connection = esDataSource.getConnection();
//        Statement statement = connection.createStatement();
//        ResultSet resultSet = statement.executeQuery(sql);
//        boolean next = resultSet.next();
//        return "query.columns()";
//    }
}
